﻿#encoding: utf-8
#this file can be used in other ruby file by loading 'wechat_api.rb'

require 'rest-client'   #to use RestClient
require 'json'          #to use JSON
require 'awesome_print' #to use ap helper

#data should be hash, not JSON string
def post_mta api_name, payload

    api = api_name.to_s
    base = 'https://totemlife.cn/apps/'
    payload.store 'badge', ENV['RDS_AGENT']

    urls={
        :assignTask => "assign-task",
    }
    url = base + urls[api_name]

    puts url
    begin
        res = RestClient.post url, payload.to_json, content_type: :json, follow_redirects: true
        return JSON.parse res.body
    rescue RestClient::ExceptionWithResponse => e
        # 处理异常
        puts e.response
    end
end

def assign_task(assignee_type = 'repCODE', assignee_id = 'DXF', task_code = 'crep-follow-npo-order',
                task_title = '请提示客户有首患订单，然后更新用户信息', channels = 'Wework',
                entry = 'https://totemlife.cn/apps/crm/create-user-profile?uid=1',
                description = 'R#2-DXF的T1会员C#6推荐新用户U#18888进入了MTA平台', status = 'pending')

    payload = {
        assignee_type: assignee_type,
        assignee_id: assignee_id,
        task_code: task_code,
        task_title: task_title,
        channels: channels,
        entry: entry,
        description: description,
        status: status
    }

    response = post_mta :assignTask, payload

end

=begin
                    $task_code = 'crep-follow-tmall-registration';
                    $task_title = "请提示客户C#$spreader_uid"."新荐用户U$uid" . "进入MTA，并更新用户背景信息";
                    $notification_channels = 'Wework Xiaoteng';
                    $task_entry =  'https://totemlife.cn/apps/crm/create-user-profile?uid=' . $uid;
                    $task_description = 'R#' . $rep_id . '-' . $rep_code."的T".$promoter_type."会员C#$spreader_uid"."推荐新用户U#$uid"."进入了TMALL小程序";
                    $task_status = 'pending';
                    $task_id = \crmeb\services\Totem::assignTask('repID', $rep_id, $task_code, $task_title, $notification_channels,$task_entry, $task_description, $task_status);

# 调用函数并打印结果

ap assign_task
=end
